﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SuperStarProject.DAO;
using SuperStarProject.Models;

namespace SuperStarProject.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult CreateSPNhanVien()
        {
            ProductModel x = new ProductModel();
            x.MaNhanVien = User.Identity.Name;
            return View(x);
        }
        [HttpPost]
        public ActionResult CreateSPNhanVien(ProductModel model)
        {
            try
            {
                ProductDAO.CreateProduct(model);
                if (model.HinhAnh == null)
                {
                    model.HinhAnh = "/Content/images/noimage.gif";
                }
                return RedirectToAction("CreateQuaTangNhanVien", "QuaTang", new { masanpham = model.MaSP });
            }
            catch (Exception ex)
            {
                return RedirectToAction("Error", "Home", new { mess = "Có lỗi xảy ra khi bạn thêm sản phẩm." });
            }
        }
        [HandleError]
        public ActionResult ShowAllProducts()
        {
            var allProducts = ProductDAO.ShowAllProducts();
            ViewData["message"] = "This is the all product page";
            return View(allProducts);
        }
        [HandleError]
        public ActionResult RandomProduct()
        {
            var allProducts = ProductDAO.ShowAllRandomProducts();
            return View(allProducts);
        }
        [HandleError]
        public ActionResult ThongKeSanPhamMuaNhieuNhat()
        {
            var allProducts = ProductDAO.ShowAllProductForStatic();
            ViewData["message"] = "This is the all product page";
            return View(allProducts);
        }
        [HandleError]
        public ActionResult HotProduct()
        {
            var allProducts = ProductDAO.ShowAllNewProducts();
            return View(allProducts);
        }
        [HandleError]
        public ActionResult SpecialProduct()
        {
            var allProducts = ProductDAO.ShowAllHotProducts();
            return View(allProducts);
        }
        [HandleError]
        public ActionResult EndedProduct()
        {
            var allProducts = ProductDAO.ShowAllEndedProducts();
            return View(allProducts);
        }

        public ActionResult ViewDetails(string productId)
        {
            try
            {
                var product = ProductDAO.ViewDetails(productId);
                return View(product);
            }
            catch (Exception ex)
            {
                ViewData["Message"] = ex.Message;
                return RedirectToAction("Error", "Home", new { mess = "Không tìm thấy sản phẩm của bạn chọn" });
            }     
        }
        public ActionResult ViewDetailsLocal(string productId)
        {
            var product = ProductDAO.ViewDetails(productId);
            return View(product);
        }

        [HandleError]
        public ActionResult ShowAllSPNhanVien()
        {
            if (!User.Identity.Name.ToUpper().StartsWith("NV"))
            {
                return RedirectToAction("Error", "Home", new { mess = "Bạn chưa đăng nhập, không xem chức năng này. Nó dành riêng cho Nhân viên" });
            }
            var allProducts = ProductDAO.ShowAllProducts();
            ViewData["message"] = "This is the all product page";
            return View(allProducts);
        }
    }
}
